package dao;

import model.Business;
import model.BusinessType;
import util.Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class BusinessDao {
    public List<Business> findAllBusiness() {
        List<Business> list = new ArrayList<Business>();
        Connection con = null;
        try {
            con = Util.getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from business as b,businessType as t where b.typeId=t.id;");
            while (rs.next()) {
                BusinessType bt = new BusinessType(rs.getInt(7), rs.getString(8), rs.getString(9));
                Business b = new Business(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getFloat(5), rs.getFloat(6), bt);
                list.add(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Util.closeConnection(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    public List<Business> findAllBusinessById(int Id){
        List<Business> list = new ArrayList<Business>();
        Connection con = null;
        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        try {
            con = Util.getConnection();
            String sql = " select * from business as b,businessType as t where b.typeId=t.id and t.id=?;";
            preparedStatement=con.prepareStatement(sql);
            preparedStatement.setInt(1,Id);
            rs=preparedStatement.executeQuery();
            while (rs.next()){
                BusinessType bt=new BusinessType(rs.getInt(7),rs.getString(8),rs.getString(9));
                Business b=new Business(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getFloat(5),rs.getFloat(6),bt);
                list.add(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Util.closeConnection(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    public List<Business> findOneBusinessById(int Id){
        List<Business> list = new ArrayList<Business>();
        Connection con = null;
        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        try {
            con = Util.getConnection();
            String sql = " select * from business where id=?";
            preparedStatement=con.prepareStatement(sql);
            preparedStatement.setInt(1,Id);
            rs=preparedStatement.executeQuery();
            while (rs.next()){
                BusinessType bt=new BusinessType(rs.getInt(4),null,null);
                Business b=new Business(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getFloat(5),rs.getFloat(6),bt);
                list.add(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Util.closeConnection(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}

